<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <h1>Heading with a <span>span</span> element.</h1>
    <p>A paragraph with <span>one</span>, <span>two</span>
        spans.</p>

    <script>
        function byTagName(node, tagName) {
            let found = [];
            tagName = tagName.toUpperCase();

            function explore(node) {
                for (let i = 0; i < node.childNodes.length; i++) {
                    let child = node.childNodes[i];
                    if (child.nodeType == document.ELEMENT_NODE) {
                        if (child.nodeName == tagName) found.push(child);
                        explore(child);
                    }
                }
            }

            explore(node);
            return found;
        }

        console.log(byTagName(document.body, "h1").length);
        // → 1
        console.log(byTagName(document.body, "span").length);
        // → 3
        let para = document.querySelector("p");
        console.log(byTagName(para, "span").length);
         // → 2
    </script>
</body>

</html>